#!/usr/bin/env bash

function browse_to {
  if (which xdg-open >/dev/null); then
    xdg-open $1
  else
    open $1
  fi
}

function hash {
  if (which sha256sum >/dev/null); then
    sha256sum
  else
    md5
  fi
}

function generate_password {
  if (which openssl >/dev/null); then
    openssl rand -hex 32
  else
    (hostname; date +%s; ls -la /) | rev | hash | base64 | head -c 32
  fi
}

if [[ -f .env ]]; then
  source .env
fi


echo "Welcome..."

if [[ -z "${gitlaboauth__client_id}${gitlaboauth__client_secret}" ]]; then
  echo ""
  echo "You need to create a GitLab Application to connect with GitLab"
  echo "Application Name: Gitter User Dev"
  echo "Redirect URI: http://localhost:5000/login/gitlab/callback"
  echo "Select the \"api\" and \"read_user\" scope"
  echo ""
  echo "Press ENTER to open GitLab"
  read nothing
  browse_to https://gitlab.com/profile/applications
  echo ""
  echo "Paste the Application ID below and press ENTER"
  read gitlaboauth__client_id
  echo "Paste the Secret below and press ENTER"
  read gitlaboauth__client_secret
  echo ""
fi

if [[ -z "${github__user_client_id}${github__user_client_secret}" ]]; then
  echo ""
  echo "You need to create a GitHub Application to connect with GitHub"
  echo "Application Name: Gitter User Dev"
  echo "Homepage URL: http://localhost:5000"
  echo "Authorisation callback URL: http://localhost:5000/login/callback"
  echo ""
  echo "Press ENTER to open GitHub"
  read nothing
  browse_to https://github.com/settings/applications/new
  echo ""
  echo "Paste the Client ID below and press ENTER"
  read github__user_client_id
  echo "Paste the Client Secret below and press ENTER"
  read github__user_client_secret
  echo ""
fi

if [[ -z "${github__client_id}${github__client_secret}" ]]; then
  echo ""
  echo "You need to do this again to create a scoped token for Private Repository access"
  echo "Application Name: Gitter Private Dev"
  echo "Homepage URL: http://localhost:5000"
  echo "Authorisation callback URL: http://localhost:5000/login/callback"
  echo ""
  echo "Press ENTER to open GitHub"
  read nothing
  browse_to https://github.com/settings/applications/new
  echo ""
  echo "Paste the Client ID below and press ENTER"
  read github__client_id
  echo "Paste the Client Secret below and press ENTER"
  read github__client_secret
  clear
fi

if [[ -z "${twitteroauth__consumer_key}${twitteroauth__consumer_secret}" ]]; then
  echo ""
  echo "We also connect with Twitter, so, yeah, you need to create a Twitter App too."
  echo "Name: Gitter Twitter YOURTWITTERUSERNAME"
  echo "Description: Connect Gitter with Twitter"
  echo "Website: http://test.gitter.im"
  echo "Callback URL: http://localhost:5000/login/twitter/callback"
  echo ""
  echo "Press ENTER to open Twitter"
  read nothing
  browse_to https://developer.twitter.com/en/apps/create
  echo "Click 'keys and tokens' to get your Consumer API Keys"
  echo "Paste the Consumer API Key below and press ENTER"
  read twitteroauth__consumer_key
  echo "Paste the Consumer API Secret Key below and press ENTER"
  read twitteroauth__consumer_secret
  clear
fi

if [[ -f .env ]]; then
  cp .env .env.backup.$(date +%s)
fi

cat <<EOF >.env
export web__sessionSecret="$(generate_password)"
export ws__superClientPassword="$(generate_password)"
export web__messageSecret="$(generate_password)"
export email__unsubscribeNotificationsSecret="$(generate_password)"
export integrations__secret="$(generate_password)"
export github__statePassphrase="$(generate_password)"
export twitteroauth__consumer_key="${twitteroauth__consumer_key}"
export twitteroauth__consumer_secret="${twitteroauth__consumer_secret}"
export gitlaboauth__client_id="${gitlaboauth__client_id}"
export gitlaboauth__client_secret="${gitlaboauth__client_secret}"
export github__client_id="${github__client_id}"
export github__client_secret="${github__client_secret}"
export github__user_client_id="${github__user_client_id}"
export github__user_client_secret="${github__user_client_secret}"
export github__anonymous_app__client_id="${github__user_client_id}"
export github__anonymous_app__client_secret="${github__user_client_secret}"
export tokens__anonymousPassword="$(generate_password)"
EOF

echo "You're good to go"
echo "Run 'source .env' to export your secret environment variables"
echo "Then run 'npm start'"
